package com.ttl.bc.ic.item;

 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.invoice.bo.BCAPInvoiceBO;
import com.ttl.bc.ic.item.BCItemBO;
import com.ttl.bc.ic.stkissue.bean.ITTL_OutStock_MinDate;
import com.ttl.bc.ic.stkissue.db.BCStkIssueDAO;
import com.ttl.common.util.CommonUtils;

public class BCItemAction extends DispatchAction {
	
	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
					
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCItemAction #mode :find");		
						
		DynaActionForm loginForm = (DynaActionForm) form;		
		String itemFind = new String(loginForm.get("itemFind").toString().getBytes("ISO8859_1"),"UTF-8");
														
//		String hql = "FROM BCItemBean WHERE (code like '%"+itemFind+"%' or name1 like '%"+itemFind+"%' ) " +
//						" and (code like '1-%' or code like '2-%' or code like '3-%' or code like '4-%' or " +
//						"	   code like '5-%' or code like '6-%' or code like '7-%' )" +
//						" ORDER BY code";
		String hql = "FROM View_BCStkLocation WHERE (itemCode like '%"+itemFind+"%' or name1 like '%"+itemFind+"%' ) " +
				" and whCode='10' and shelfCode = '10' "+
				" ORDER BY itemCode";		
		
		new BCItemBO().setPage(request, hql);
		
		request.setAttribute("itemFind", itemFind);
		
		return mapping.findForward("success_find");
	}
	
	public ActionForward docNoFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCItemAction #mode :docNoFind");		
		String docNoType = (String)request.getParameter("docNoType");
		String line = (String)request.getParameter("line");
		
		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind);
		}
		request.setAttribute("docNoType", docNoType);
		request.setAttribute("line", line);		
		
		
		String hql = "";
		
		hql = "FROM BCStkIssueSub2Query WHERE itemCode = '"+itemFind+"' and (ittl_status=0 or ittl_status is null) ORDER BY docNo";
				
		System.out.println(hql);
		new BCAPInvoiceBO().setPage(request, hql);		
		
		return mapping.findForward("success_docno_popup");				
				
	}
	
	public ActionForward pack(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
					
		if(new BCItemBO().pack(mapping, form, request, response)) {
		
			return mapping.findForward("success_find_pack");
		}else {
			return mapping.findForward("failure_find_pack");
		}
	}
	
	public ActionForward viewpack(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
					
		if(new BCItemBO().viewpack(mapping, form, request, response)) {
		
			return mapping.findForward("success_view_pack");
		}else {
			return mapping.findForward("failure_view_pack");
		}
	}	
	public ActionForward closedDate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
					
		ITTL_OutStock_MinDate m = new BCStkIssueDAO().getMinDate();
		request.setAttribute("minDate", CommonUtils.convertDateToString(m.getMinDate()) );
		
		return mapping.findForward("success_closed_date");
		
	}	
	public ActionForward updateClosedDate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
					
		if(new BCStkIssueDAO().updateClosedDate(form, request)) {
			request.setAttribute("message", "แก้ไขข้อมูลเรียบร้อย");
			request.setAttribute("pageForward", "<meta http-equiv='refresh' content='2; url=stock.do?mode=closedDate' >");
			
			
			
			System.out.println("success_update_closed_date");
			return mapping.findForward("success_update_closed_date");
		}else {
			System.out.println("failure_update_closed_date");
			return mapping.findForward("failure_update_closed_date");
		}
	}	
	
	public ActionForward updatepack(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
					
		if(new BCItemBO().updatepack(mapping, form, request, response)) {
		
			return mapping.findForward("success_update_pack");
		}else {
			return mapping.findForward("failure_update_pack");
		}
	}	
}
